﻿Module mdl_serial

    Private SALTKey As String = "kimochi" & Now.Year

    Function GenerateKey(ByVal KodeSekolah As String) As String
        If SALTKey.Length > 16 Then
            'limitation of the encryption mechanism
            SALTKey = SALTKey.Substring(0, 16)
        End If

        If KodeSekolah.Trim.Length = 0 Then
            'the Text to encrypt not set!!!
            Return String.Empty
        End If

        Dim skey As New Serial.Data(SALTKey)
        Dim sym As New Serial.Symmetric(Serial.Symmetric.Provider.Rijndael)
        Dim objEncryptedData As Serial.Data
        objEncryptedData = sym.Encrypt(New Serial.Data(KodeSekolah), skey)

        Dim tmpKey As String = objEncryptedData.ToHex
        Dim key1 As String
        Dim key2 As String
        Dim key3 As String

        key1 = tmpKey.Substring(1, 4)
        key2 = tmpKey.Substring(8, 4)
        key3 = tmpKey.Substring(15, 4)

        Return key1 & "-" & key2 & "-" & key3
    End Function

End Module
